#include "MapGrid.h"

MapGrid* MapGrid::m_sharedMapGrid = NULL;

MapGrid::MapGrid()
{
	m_arry = CCArray::createWithCapacity(SLOT_ROW_COUNT);
	m_arry->retain();
	for(unsigned int i =0; i<SLOT_ROW_COUNT;i++){
		CCArray *ary = CCArray::createWithCapacity(SLOT_COLUMN_COUNT);
		for (unsigned int j=0;j<SLOT_COLUMN_COUNT;j++)
		{
				ary->addObject(MapSlot::Create(i,j));
		}
	
		m_arry->addObject(ary);
	}
}

MapGrid::~MapGrid(void)
{
}

MapGrid* MapGrid::GetSharedCache()
{
	if(m_sharedMapGrid == NULL){
		m_sharedMapGrid = new MapGrid();
		m_sharedMapGrid->autorelease();
		m_sharedMapGrid->retain();
	}

	return m_sharedMapGrid;
}

void MapGrid::ClearSharedCache()
{
	m_sharedMapGrid->release();
	m_sharedMapGrid = NULL;
}

CCRect MapGrid::Rect()
{
	return CCRectMake(OFFSET_X, OFFSET_Y, WIDTH, HEIGHT);
}

CCRect MapGrid::ExtendRect()
{
	return CCRectMake(OFFSET_X, OFFSET_Y, WIDTH + MapSlot::WIDTH, HEIGHT);
}

MapSlot* MapGrid::GetSlotByKey( string key )
{
	for(unsigned int i =0; i<m_arry->count();i++){

		CCArray* ary = (CCArray*)m_arry->objectAtIndex(i);
		for (unsigned int j=0;j<ary->count();j++)
		{
			MapSlot* slot = (MapSlot*)ary->objectAtIndex(j);
			if(slot->GetKey() == key ){
				return slot;
			}
		}
	}

	return NULL;
}

MapSlot* MapGrid::SlotOverlapped( CCPoint* cp )
{
	if(Rect().containsPoint(*cp)){
		for(unsigned int i =0; i<m_arry->count();i++){

			CCArray* ary = (CCArray*)m_arry->objectAtIndex(i);
			for (unsigned int j=0;j<ary->count();j++)
			{
				int slotoffsetx = OFFSET_X +  MapSlot::WIDTH * j;
				int slotoffsety = OFFSET_Y +  MapSlot::HEIGHT * i;
				if(cp->x >= slotoffsetx 
					&& cp->x <= slotoffsetx + MapSlot::WIDTH
					&& cp->y >= slotoffsety
					&& cp->y <= slotoffsety + MapSlot::HEIGHT){
					MapSlot* slot = (MapSlot*)ary->objectAtIndex(j);
					if(slot->CheckOverlap(cp) ){
						return slot;
					}
				}
			}
		}
	}

	return NULL;
}

bool MapGrid::containsTouchLocation(CCTouch* touch)
{
	return Rect().containsPoint(touch->getLocation());
}

cocos2d::CCPoint MapGrid::Set2Slot( MapSlot* slot )
{
	slot->Set();
	return GetSlotCoordinate(slot);
}

cocos2d::CCPoint MapGrid::GetSlotCoordinate( MapSlot* slot )
{
	return MapGrid::GetSlotCoordinate(slot->Row(),slot->Column());
}

cocos2d::CCPoint MapGrid::GetSlotCoordinate( int row, int column )
{
	return ccp(OFFSET_X + MapSlot::WIDTH * column + MapSlot::WIDTH/2,OFFSET_Y + MapSlot::HEIGHT * row + MapSlot::HEIGHT/2);
}

cocos2d::CCPoint MapGrid::GetNextSlotCoordinate( MapSlot* slot )
{
	int row = slot->Row();
	int column = slot->Column() + 1;
	return MapGrid::GetSlotCoordinate(row,column);
}

cocos2d::CCPoint MapGrid::GetOutRangeCoordinate( MapSlot* slot )
{
	int row = slot->Row();
	int column = SLOT_COLUMN_COUNT + 1;
	return MapGrid::GetSlotCoordinate(row,column);
}

bool MapGrid::IsOutOfRange( CCPoint point )
{
	return !ExtendRect().containsPoint(point);
}

cocos2d::CCPoint MapGrid::GetLeftOutRange()
{
	return ccp(OFFSET_X-1,OFFSET_Y-1);
}

cocos2d::CCPoint MapGrid::GetRightOutRange()
{
	return GetSlotCoordinate(0,SLOT_COLUMN_COUNT+1);
}

int MapGrid::GetRowCoordinate( int row )
{
	return GetSlotCoordinate(row,0).y;
}

int MapGrid::GetColumnCoordinate( int column )
{
	return GetSlotCoordinate(0,column).x;
}
